package com.zq.assistant.ui.file;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zq.assistant.R;
import com.zq.assistant.app.Config;
import com.zq.assistant.bean.Result;
import com.zq.assistant.func.apifactory.callback.ResultCallback;
import com.zq.assistant.func.okhttp.SpeedFileTransferListener;
import com.zq.assistant.func.okhttp.upload.UploadInfo;
import com.zq.assistant.ui.BaseActivity;
import com.zq.assistant.utils.AppUtils;
import com.zq.assistant.utils.JsonUtils;
import com.zq.assistant.utils.OKHttpUtils;
import com.zq.assistant.utils.ObjectCallback;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by zhangqiang on 17-6-5.
 */

public class FileUploadActivity extends BaseActivity {

    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.bt_upload)
    Button btUpload;
    @InjectView(R.id.bt_post)
    Button btPost;
    @InjectView(R.id.pb_upload)
    ProgressBar pbUpload;
    @InjectView(R.id.tv_upload_speed)
    TextView tvUploadSpeed;
    @InjectView(R.id.bt_download)
    Button btDownload;
    @InjectView(R.id.pb_download)
    ProgressBar pbDownload;
    @InjectView(R.id.tv_download_speed)
    TextView tvDownloadSpeed;

    public static void start(Context context) {

        Intent intent = new Intent(context, FileUploadActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_upload);
        ButterKnife.inject(this);

        initToolbar(this,toolbar);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1000 && resultCode == RESULT_OK) {
            String imagePath = AppUtils.getImagePathFromActivityResult(this, data);

            OKHttpUtils.upload(Config.getHOST() + "/file/getFileList", Arrays.asList(new File(imagePath)), new UploadInfo(), new SpeedFileTransferListener() {
                @Override
                public void onTransfer(final long contentLength, final long currentLength) {
                    super.onTransfer(contentLength, currentLength);
                    Log.i("Test", "contentLength : " + contentLength + "****currentLength : " + currentLength);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pbUpload.setProgress((int) ((float) currentLength / contentLength * 100));
                        }
                    });
                }

                @Override
                public void onFinish(String url) {
                    super.onFinish(url);
                    Log.i("Test", "***********onFinish");
                }

                @Override
                public void onSpeedChange(final long bytesPerSecond) {
                    Log.i("Test", "***********onSpeedChange:" + bytesPerSecond / 1024 + "kb/s");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvUploadSpeed.setText(AppUtils.getFileTransferSpeedDisplay(bytesPerSecond));
                        }
                    });

                }
            });
        }
    }


    @OnClick({R.id.bt_upload, R.id.bt_post})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_upload:

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                intent.setType("image/*");
                intent.setType("video/*");

                startActivityForResult(intent, 1000);


                break;
            case R.id.bt_post:

                Map<String, String> params = new HashMap<>();
                params.put("name", "zhangqiang");
                params.put("pwd", "123456");
                OKHttpUtils.postForm(Config.getHOST() + "/file/getFileList", params, null, new ObjectCallback(Result.class, new ResultCallback<Result<String>>() {
                    @Override
                    public void onFail(int errorCode, String message) {
                        Log.i("Test", "==onFail======" + message);
                    }

                    @Override
                    public void onSuccess(Result<String> result) {
                        Log.i("Test", "===onSuccess=====" + JsonUtils.toJsonString(result));
                    }
                }));
                break;
        }
    }

    @OnClick(R.id.bt_download)
    public void onViewClicked() {

        String savePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/download";

        OKHttpUtils.download(Config.getHOST() + "/file/download?path=/home/zhangqiang/fileUpload/10/15/ee6b4649-cacf-4289-b705-9ccd2d1f564a_周杰伦 - 告白气球.mp4",
                savePath,
                null,
                new SpeedFileTransferListener() {
                    @Override
                    public void onTransfer(final long contentLength, final long currentLength) {
                        super.onTransfer(contentLength, currentLength);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pbDownload.setProgress((int) ((float) currentLength / contentLength * 100));
                            }
                        });
                    }

                    @Override
                    public void onFinish(String url) {
                        super.onFinish(url);
                    }

                    @Override
                    public void onSpeedChange(final long bytesPerSecond) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvDownloadSpeed.setText(AppUtils.getFileTransferSpeedDisplay(bytesPerSecond));
                            }
                        });
                    }
                });
    }


}
